#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include "./utils/atoi.h"
#include "./utils/atol.h"
#include "./utils/atof.h"
#include "./enum_values_def_on_header/enum_values_def_on_header.c"

char* longestCommonPrefix(char** strs, int strsSize)
{
	if (strsSize <= 1)
		return "";
	int pos = 0;
	while (1)
	{
		char f = strs[0][pos];
		if (!f)
			break;
		int i = 1;
		for (; i < strsSize; ++i)
		{
			if (strs[i][pos] != f)
				break;
		}
		if (i != strsSize)
			break;
		++pos;
	}
	if (!pos)
		return "";
	int l = sizeof(char) * (pos + 1);
	char* ret = malloc(l);
	memcpy(ret, strs[0], l);
	ret[pos] = 0;
	return ret;
}

int main()
{
	TestEnum a = TEST_ENUM_a;
	TestEnum b = TEST_ENUM_b;
	TestEnum c = TEST_ENUM_c;
	TestEnum d = TEST_ENUM_d;
	printf("TEST_ENUM: \"%s\" = %d\n", "TEST_ENUM_a", a);
	printf("TEST_ENUM: \"%s\" = %d\n", "TEST_ENUM_b", b);
	printf("TEST_ENUM: \"%s\" = %d\n", "TEST_ENUM_c", c);
	printf("TEST_ENUM: \"%s\" = %d\n", "TEST_ENUM_d", d);


	char* msg = "-100901";
	printf("atoi: \"%s\" ===> %d\n", msg, gim_atoi(msg, strlen(msg)));
	printf("atol: \"%s\" ===> %ld\n", msg, gim_atol(msg, strlen(msg)));
	printf("atof: \"%s\" ===> %f\n", msg, gim_atof(msg, strlen(msg)));
	char* l[3] = { "rdog", "racecar", "rrcar" };
	printf("%s\n", longestCommonPrefix(l, 3));

	char str[100];
	char stopped = 0;
	while (!stopped)
	{
		printf("input: ");
		scanf("%s", &str);
		if (!strcmp(str, "exit"))
		{
			stopped = 1;
		}
		else
		{
			printf("atoi: \"%s\" ===> %d\n", str, gim_atoi(str, strlen(str)));
			printf("atol: \"%s\" ===> %ld\n", str, gim_atol(str, strlen(str)));
			printf("atof: \"%s\" ===> %f\n", str, gim_atof(str, strlen(str)));
		}
	}
	printf("exit!\n");
	return 0;
}
